<template>
  <div>
    <i class="btn iconfont icon-hexagon" @click="changeFrame"></i>
    <div class="btn user-op">
      <i class="iconfont icon-user"></i>
      <span>{{ admin.admin_name }}</span>
      <i class="iconfont icon-chevron-down"></i>
      <ul>
        <li @click="showUserInfo"><i class="iconfont icon-alert-circle"></i>个人信息</li>
        <li @click="logout"><i class="iconfont icon-log-out"></i>退出登录</li>
      </ul>
    </div>
    <el-dialog title="个人信息" :visible.sync="dialogVisible">
      <el-form label-width="120px">
        <el-form-item label="用户名">
          <span>{{ admin.admin_name }}</span>
        </el-form-item>
        <el-form-item label="管理员类型">
          <span>{{getAdminType(admin.admin_type)}}</span>
        </el-form-item>
        <el-form-item label="联系方式">
          <span>{{ admin.admin_phone }}</span>
        </el-form-item>
        <el-form-item label="备注">
          <span>{{ admin.remark }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      admin:this.$cookies.get("admin")|| this.$store.getters.user,
      dialogVisible:false,
      type:["超级管理员","普通管理员"]
    };
  },
  methods: {
    logout() {
      this.$confirm('是否确认退出该账号?','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        this.$cookies.remove("admin");
        PUZZLE_CONFIG.pageHome = "login";
        this.$router.replace({ name: PUZZLE_CONFIG.pageHome });
      })
    },
    showUserInfo(){
      this.dialogVisible = true;
    },
    getAdminType(type){
      return this.type[type]
    },
    changeFrame() {
      const frames = {
        dashboard: {
          name: "dashboard",
          version: "0.1.0",
          host: "/"
        },
        demo: {
          name: "demo",
          version: "0.1.0"
        }
      };

      if (!sessionStorage.getItem("frame"))
        sessionStorage.setItem("frame", JSON.stringify(frames.demo));
      else {
        let name = JSON.parse(sessionStorage.getItem("frame")).name;
        if (name === "dashboard")
          sessionStorage.setItem("frame", JSON.stringify(frames.demo));
        if (name === "demo")
          sessionStorage.setItem("frame", JSON.stringify(frames.dashboard));
      }
      location.reload();
    }
  }
};
</script>

<style lang="less">
@import "~@frames/dashboard/style/var";

#topbarBtns {
  height: @topBarH;
  padding-right: 15px;

  .btn {
    display: inline-block;
    padding: 0 12px;
    font-size: 21px;
    line-height: @topBarH;
    color: #fff;
    cursor: pointer;

    transition: all 0.2s;

    &:hover {
      background: rgba(255, 255, 255, 0.1);
    }
  }

  .user-op {
    position: relative;
    margin-left: 19px;
    cursor: default;

    &:hover {
      ul {
        visibility: visible;
        opacity: 1;
        transform: translateY(0);
      }
    }

    &:after {
      position: absolute;
      content: "";
      left: -10px;
      background: rgba(0, 0, 0, 0.07);
      top: 10px;
      bottom: 10px;
      width: 1px;
    }

    .icon-user {
      display: inline-block;
      vertical-align: top;
      font-size: 21px;
      line-height: @topBarH;
      color: #fff;
    }

    span {
      display: inline-block;
      padding: 0 6px;
      font-size: 14px;
      vertical-align: top;
    }

    .icon-chevron-down {
      vertical-align: middle;
      margin-top: -5px;
      font-size: 12px;
      font-weight: bold;
    }

    ul {
      position: absolute;
      top: @topBarH;
      right: 0;
      left: 0;
      background: #fff;
      box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.05);
      color: #444;
      padding: 8px 0;
      border-radius: 0 0 3px 3px;

      transition: all 0.2s;
      visibility: hidden;
      opacity: 0;
      transform: translateY(10px);
      li {
        text-align: center;
        font-size: 13px;
        font-weight: bold;
        line-height: 34px;
        i {
          margin-right: 6px;
          font-size: 13px;
        }
        &:hover {
          background: @hover;
        }
      }
    }
  }
}
</style>
